package vue;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import lib.Element;
import lib.Ensemble;

import vue.composants.Boutton;

public class RemplissageAleatoireDialog extends JDialog implements ActionListener{

	private FenetrePrincipale mere;
	private Boutton bouttonRemplir;
	private JComboBox EnsembleCombo;
	private JLabel text;
	private JTextField rep;
	
	public RemplissageAleatoireDialog(FenetrePrincipale mere)
	{
		super(mere);
		this.setMere(mere);
		this.setBounds(500, 400, 500, 500);
		
		if(this.mere.getMesEnsemble().isEmpty())
		{
			new NouvelEnsembleDialog(this.getMere());
		}
		else
		{
			this.text = new JLabel("Nombre de valeur : ");
			this.rep = new JTextField(5);
			
			this.EnsembleCombo = new JComboBox(this.getMere().getMesEnsemble().toArray());
			
			this.bouttonRemplir = new Boutton("Remplir avec des valeurs aleatoire");
			this.bouttonRemplir.addActionListener(this);		
			
			this.getContentPane().setLayout(new GridLayout());
			this.getContentPane().add(this.text);
			this.getContentPane().add(this.rep);
			this.getContentPane().add(this.EnsembleCombo);
			this.getContentPane().add(this.bouttonRemplir);
			
			this.pack();
			this.setVisible(true);
		}
	}

	/**
	 * @return the mere
	 */
	public FenetrePrincipale getMere() {
		return mere;
	}

	/**
	 * @param mere the mere to set
	 */
	public void setMere(FenetrePrincipale mere) {
		this.mere = mere;
	}
	
	private void remplirEnsemble()
	{
		Ensemble e = (Ensemble)this.EnsembleCombo.getSelectedItem();
		int nb = Integer.parseInt(this.rep.getText());
		while(nb>=0)
		{
			e.ajoutElement(new Element((int)(Math.random()*20)));
			--nb;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.bouttonRemplir)
		{
			this.remplirEnsemble();
		}
	}
}
